<?php
/**
 * 后台同样控制器，用于实现通用方法以及前置后置方法等
 * Enter description here ...
 * @author Code.404
 *
 */
import('@.ORG.FAC');
import('ORG.Util.Input');
require LIB_PATH ."Action/CoreAction.class.php";
class CommonAction extends CoreAction{
	
	protected $_a = 'show';//要执行的动作
	protected $_t = 'page';//显示的类型
	
	/**
	 * 生成验证码
	 * Enter description here ...
	 */
	public function verifyCode(){
		import("ORG.Util.Image");
		Image::buildImageVerify(4,1,png,50,23);
	}
	/**
	 * 验证验证码正确性
	 * Enter description here ...
	 */
	public function checkVeirfy(){
		if( $_SESSION['verify'] != md5($_REQUEST['verifyCode'])){
			$this->ajaxReturn('VerifyError',L('VERIFY_CODE_ERROR'),0);
		}else{
			$this->ajaxReturn('Success',L('VERIFY_CODE_OK'),1);
		}
	}

	/**
	 * 删除操作
	 * Enter description here ...
	 * @param unknown_type $model
	 * @param unknown_type $id
	 * @param unknown_type $redirect_url
	 */
	protected function delete($model , $id , $redirect_url = '' ,$callbackType = 'forward' , $navTabId = 0){
		try{
			$id = FAC::F($id,FAC::INT);

			if(D($model)->delete($id)){
				$this->ajaxJsonReturn(200 , L('ACTION_SUCCESS'),$navTabId,$redirect_url,$callbackType);
			}
			$this->ajaxJsonReturn(300 ,L('ACTION_FAILED'));
		}catch (Exception $e){
			$this->ajaxJsonReturn(300,L($e->getMessage()));
		}
	}
	/**
	 * 一键修改
	 * Enter description here ...
	 * @param unknown_type $model
	 * @param unknown_type $action
	 * @param unknown_type $id
	 * @param unknown_type $redirect_url
	 * @param unknown_type $callbackType
	 * @param unknown_type $navTabId
	 */
	protected function _one_touch_action($model ,$action , $id , $redirect_url = '' ,$callbackType = 'forward' , $navTabId = 0){
		try{
			$id = FAC::F($id,FAC::INT);

			if(D($model)->{$action}($id)){
				$this->ajaxJsonReturn(200 , L('ACTION_SUCCESS'),$navTabId,$redirect_url,$callbackType);
			}
			$this->ajaxJsonReturn(300 ,L('ACTION_FAILED'));
		}catch (Exception $e){
			$this->ajaxJsonReturn(300,L($e->getMessage()));
		}
	}
	/**
	 * 图片上传处理
	 * Enter description here ...
	 */
	protected function imageUpload($removeOrigin = true){
		import("ORG.Net.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath = './Data/upload/images/';
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        // 设置引用图片类库包路径
        $upload->imageClassPath = 'ORG.Util.Image';
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = 'm_';  //生产2张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = C('FR_LINKIMAGE_WIDTH');
        //设置缩略图最大高度
        $upload->thumbMaxHeight = C('FR_LINKIMAGE_HEIGHT');
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //删除原图
        $upload->thumbRemoveOrigin = $removeOrigin;
        if (!$upload->upload()) {
            //捕获上传异常
            throw_exception($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            return $uploadList[0]['savename'];
        }
	}
	/**
     +----------------------------------------------------------
	 * 根据表单生成查询条件
	 * 进行列表过滤
     +----------------------------------------------------------
	 * @access protected
     +----------------------------------------------------------
	 * @param Model $model 数据对象
	 * @param HashMap $map 过滤条件
	 * @param string $sortBy 排序
	 * @param boolean $asc 是否正序
     +----------------------------------------------------------
	 * @return void
     +----------------------------------------------------------
	 * @throws ThinkExecption
     +----------------------------------------------------------
	 */
	protected function _list($model, $map, $order = '') {
		//取得满足条件的记录数
		$count = $model->where ( $map )->count ( 'id' );
		if ($count > 0) {
			import ( "@.ORG.Page" );
			//创建分页对象
			if (! empty ( $_REQUEST ['listRows'] )) {
				$listRows = $_REQUEST ['listRows'];
			} else {
				$listRows = C('PAGE_NUM_ADMIN');
			}
			$p = new Page ( $count, $listRows );
			//分页查询数据
			$voList = $model->where($map)->order( $order )->limit($p->firstRow . ',' . $p->listRows)->select ( );

			//echo $model->getlastsql();
			//分页跳转的时候保证查询条件
			foreach ( $map as $key => $val ) {
				if (! is_array ( $val )) {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}
			//分页显示
			$page = $p->show ();

			//模板赋值显示
			$this->assign ( 'list', $voList );

			$this->assign ( "page", $page );
		}
		
		//zhanghuihua@msn.com
		$this->assign ( 'totalCount', $count );
		$this->assign ( 'numPerPage', $p->listRows );
		$this->assign ( 'currentPage', !empty($_REQUEST[C('VAR_PAGE')])?$_REQUEST[C('VAR_PAGE')]:1);

		return;
	}
}